package com.moyq5.mvc.maker.test.feign.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.mvc.maker.test.bean.entity.TraCost;
import com.moyq5.mvc.maker.test.bean.search.TraCost4Search;
import com.moyq5.mvc.maker.test.feign.server.jackson.filter.TraCostFilter;
import com.moyq5.mvc.maker.test.core.service.TraCostService;


/**
 * @author Moyq5’s Tools
 * @since 2019-06-17 18:52:23
 */
@RestController
@RequestMapping("/cost")
public class TraCostController {
	
	private static Logger log = LoggerFactory.getLogger(TraCostController.class);
	
	@Autowired
	private TraCostService costService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param costId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long costId, String filter) {
		log.debug(">>>>oneById: costId={},filter={}", costId, filter);
		TraCost cost = costService.one(costId);
		ObjectResult<TraCost> objRst = new ObjectResult<TraCost>();
		objRst.setData(cost);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(TraCostFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param cost4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<TraCost4Search, String> cost4s) {
		log.debug(">>>>count: ");
		Long count = costService.count(cost4s.getEntity(), cost4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param cost4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<TraCost4Search, Sorting> cost4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		TraCost cost = costService.one(cost4s.getEntity(), cost4s.getCustom());
		ObjectResult<TraCost> objRst = new ObjectResult<TraCost>();
		objRst.setData(cost);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(TraCostFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param cost4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<TraCost4Search, Sorting> cost4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<TraCost> list = costService.list(cost4s.getEntity(), cost4s.getCustom());
		ListResult<TraCost> listRst = new ListResult<TraCost>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(TraCostFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param cost4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<TraCost4Search, Paging> cost4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<TraCost> page = costService.page(cost4s.getEntity(), cost4s.getCustom());
		PageResult<TraCost> pageRst = new PageResult<TraCost>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(TraCostFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param from
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody TraCost from) {
		log.debug(">>>>add: TraCost");
		TraCost cost = costService.persist(from);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(cost.getCostId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody TraCost form) {
		log.debug(">>>>update: TraCost");
		TraCost cost = costService.one(form.getCostId());
		EntityUtils.combine(form, cost);
		costService.merge(cost);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param costId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long costId) {
		log.debug(">>>>delete: costId={}", costId);
		costService.remove(costId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
}
